import { useEffect, useRef, RefObject } from 'react';
type BusinessOption = {
  state: boolean,
  fn:() => void
}
// 自定义Hook，用于检测元素是否出现在视窗中
export function useScrollLoadRef( aOptions: BusinessOption, options: IntersectionObserverInit): RefObject<HTMLDivElement> {
  const ref = useRef(null);
  const {state, fn} = aOptions
  useEffect(() => {
    const observer = new IntersectionObserver(([entry]) => {
      // 更新父组件的状态
      if (entry.isIntersecting && state) {
        fn()
      }
    }, options);

    if (ref.current) {
      observer.observe(ref.current);
    }

    return () => {
      observer.disconnect();
    };
  }, [state, options]);

  return ref;
}